﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CPA
{
    class fmv_02_startmenu
    {
        Texture2D[] sprites;
        SpriteBatch spriteBatch;
        char selected = (char)0;
        SpriteFont[] fonts;
        byte menuItem = 0;
        bool down = false, up = false, a = false;
        GameState gameState;
        Song opening;
        bool play = true;
        

        public fmv_02_startmenu(SpriteBatch spriteBatch, Texture2D[] textures, SpriteFont[] fonts, Song opening )
        {
            this.spriteBatch = spriteBatch;
            this.sprites = textures;
            this.fonts = fonts;
            this.selected = (char)0;
            this.gameState = GameState.StartMenu;
            this.opening = opening;
        }

        public GameState get_state()
        {
            return this.gameState;
        }

        public void update()
        {
            KeyboardState state = Keyboard.GetState();
            GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

            if (play)
            {
                MediaPlayer.Play(opening);
                play = false;
            }

            switch (menuItem)
            {
                // Options
                case 3: break;
                // New Game
                case 2: break;
                // Load Game
                case 1: break;
                // Main Menu
                case 0:
                default:
                    #region Main Menu Controls
                    // Pressing Down
                    if ((state.IsKeyDown(Keys.Down) || (gamePadState.DPad.Down == ButtonState.Pressed)) && !down)
                    {
                        selected++;
                        if (selected == (char)4)
                        {
                            selected = (char)1;
                        }
                        down = true;
                    }
                    if ((state.IsKeyUp(Keys.Down) && (gamePadState.DPad.Down == ButtonState.Released)))
                    {
                        down = false;
                    }

                    // Pressing Up
                    if ((state.IsKeyDown(Keys.Up) || (gamePadState.DPad.Up == ButtonState.Pressed)) && !up)
                    {
                        selected--;
                        if (selected == (char)0)
                        {
                            selected = (char)3;
                        }
                        up = true;
                    }

                    if ((state.IsKeyUp(Keys.Up) && (gamePadState.DPad.Up == ButtonState.Released)))
                    {
                        up = false;
                    }

                    // Pressing Confirm
                    if ((state.IsKeyDown(Keys.Enter) || (gamePadState.Buttons.A == ButtonState.Pressed)) && !a)
                    {
                        menuItem = (byte)selected;
                        a = true;
                        if (selected == (char)1)
                        {
                            gameState = GameState.FmvIntro;
                            MediaPlayer.Stop();
                        }
                        else
                        {
                            gameState = GameState.StartMenu;
                        }
                    }
                    if ((state.IsKeyUp(Keys.Enter) && (gamePadState.Buttons.X == ButtonState.Released)))
                    {
                        a = false;
                    }

                    #endregion
                    break;
            }
        }

        public void draw()
        {
            spriteBatch.Draw(sprites[0], new Rectangle(0, 0, 800, 480), Color.White);
            switch( menuItem )
            {
                    // Options
                case 3: break;
                    // New Game
                case 2: break;
                    // Load Game
                case 1: break;
                    // Main Menu
                case 0:
                default:
                    #region Selected Swtich
                    switch ( selected )
                    {
                        case (char)2:
                            spriteBatch.DrawString(fonts[0], "New Game", new Vector2(350, 300), Color.Crimson);
                            spriteBatch.DrawString(fonts[1], "Load Game", new Vector2(350, 325), Color.Aqua);
                            spriteBatch.DrawString(fonts[0], "Options", new Vector2(350, 355), Color.Crimson);
                            break;
                        case (char)3:
                            spriteBatch.DrawString(fonts[0], "New Game", new Vector2(350, 300), Color.Crimson);
                            spriteBatch.DrawString(fonts[0], "Load Game", new Vector2(350, 325), Color.Crimson);
                            spriteBatch.DrawString(fonts[1], "Options", new Vector2(350, 350), Color.Aqua);
                            break;
                        case (char)1:
                        default:
                            spriteBatch.DrawString(fonts[1], "New Game", new Vector2(350, 300), Color.Aqua);
                            spriteBatch.DrawString(fonts[0], "Load Game", new Vector2(350, 330), Color.Crimson);
                            spriteBatch.DrawString(fonts[0], "Options", new Vector2(350, 355), Color.Crimson);
                            break;
                    }
                    #endregion

                    break;
            }
        }
    }
}
